﻿//
//
/////////////////////////////////////////////////////////////////////////////
using FreeSpace.Logic.Worker;
using FreeSpaceLibrary;
using FreeSpaceLibrary.Objects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

/////////////////////////////////////////////////////////////////////////////
namespace FreeSpace.Scenes
{
    /////////////////////////////////////////////////////////////////////////
    public class GameScene : BaseScene
    { 
        private BackgroundSubScene _backgroundScene;
        private SpaceShip _spaceShip;
        private TargetsField _targets;

        /////////////////////////////////////////////////////////////////////
        public GameScene(SpriteBatch sprite)
            :base(sprite)
        {
            _spaceShip = new SpaceShip(0.3f, sprite, 90) {Position = new Vector2(Constants.ScreenWidth / 2, Constants.ScreenHeight - 50)};

            _targets = new TargetsField(sprite);
            _targets.AddEnemyes(ContentLoader.GetTargets());
            object[] y = ContentLoader.GetTargetsByLevel(Levels.Level1, SubLevels.SubLevel1);
        }

        /////////////////////////////////////////////////////////////////////
        public override void Start()
        {
            _backgroundScene = new BackgroundSubScene(_sprite);
            base.Start();
        }

        /////////////////////////////////////////////////////////////////////
        public override void Update(GameTime gameTime, Microsoft.Xna.Framework.Input.KeyboardState keyboardState)
        {
            if (IsActive)
            {
                if (_backgroundScene != null)
                {
                    _backgroundScene.Update(gameTime, keyboardState);
                }
                
                _calculateCollisions();

                _targets.Update(gameTime, keyboardState);
                _spaceShip.Update(gameTime, keyboardState);
            }
            base.Update(gameTime, keyboardState);
        }

        /////////////////////////////////////////////////////////////////////
        public override void Draw()
        {
            if (IsActive)
            {
                if (_backgroundScene != null)
                {
                    _backgroundScene.Draw();
                }
                _targets.Draw();
                _spaceShip.Draw();
            }
            base.Draw();
        }

        /////////////////////////////////////////////////////////////////////
        private void _calculateCollisions()
        {
            for (int i = 0; i < _spaceShip.SimpleWeapons.Count; i++)
            {
                WeaponSimple weapon = _spaceShip.SimpleWeapons[i];
                if (weapon.InScreen)
                {
                    SpaceObject collisionTarget = _targets.IsColissionExists(weapon);
                    if (collisionTarget != null)
                    {
                        _targets.Remove(collisionTarget);
                        _spaceShip.SimpleWeapons.Remove(weapon);
                    }
                }
            }
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//
